# Genesis Plus - Sega Mega Drive emulator
# (c) 1999, 2000, 2001, 2002, 2003  Charles MacDonald
# Sega Dreamcast port (c) 2004 Stuart Dalton
# Experimental (hardware accelerated) version

NAME = genplus-x
TARGET = $(NAME).elf
BIN = $(NAME).bin
FIRSTREAD = 1st_read.bin

OBJS = genesis.o vdp.o unzip.o fileio.o loadrom.o io.o mem68k.o memz80.o membnk.o memvdp.o sram.o
OBJS += sound.o sn76496.o
OBJS += error.o font.o main.o menu.o dc_sram.o cpu68k.o
PROBJS = cz80.o

CDRECORD_ID = /dev/hdc
CDRECORD_DEV = /dev/hdc

all: rm-elf $(TARGET)

include $(KOS_BASE)/Makefile.rules

KOS_LOCAL_CFLAGS = -Os -I. -I.. -I../cpu -I../sound -I../cz80 -DLSB_FIRST -DALIGN_LONG -DCPUZ80_USE_CZ80 -DBUILD_YM2203

OBJS += xsystem.o xfm.o
#OBJS += system.o fm.o

KOS_LOCAL_CFLAGS += -DXVDP
OBJS += xrender.o

KOS_LOCAL_CFLAGS += -I../c68k -I../fame
PROBJS += c68k.o c68kexec.o fame.o

#KOS_LOCAL_CFLAGS += -DUSE_ROMDISK
#OBJS += romdisk.o

# Sound system
# Include the ARM sound driver from a more recent version of KOS
# Needed for the syncronised channels
PROBJS += snd_stream_drv.o
snd_stream_drv.o: arm/stream.drv
	$(KOS_BASE)/utils/bin2o/bin2o $< snd_stream_drv snd_stream_drv.o
arm/stream.drv:
	make -C arm

COMPILER=$(KOS_CC)

clean:
	rm -f $(OBJS) $(TARGET) $(BIN) $(FIRSTREAD) romdisk.img

rclean:
	rm -f $(OBJS) $(PROBJS) $(TARGET) $(BIN) $(FIRSTREAD) romdisk.img $(GSRC) ../m68k/m68kmake

rm-elf:
	-rm -f $(TARGET) $(BIN) $(FIRSTREAD)

$(TARGET): $(OBJS) $(PROBJS)
	$(COMPILER) $(KOS_CFLAGS) $(KOS_LDFLAGS) -o $(TARGET) $(KOS_START) \
		$(OBJS) $(PROBJS) -L$(KOS_BASE)/lib -lm -lz -lpng $(KOS_LIBS)

%.o: %.c
	$(COMPILER) $(KOS_CFLAGS) $(KOS_LOCAL_CFLAGS) -D_arch_$(KOS_ARCH) $(KOS_ALL_INCS) -c $< -o $@

%.o: ../%.c
	$(COMPILER) $(KOS_CFLAGS) $(KOS_LOCAL_CFLAGS) -D_arch_$(KOS_ARCH) $(KOS_ALL_INCS) -c $< -o $@

%.o: ../c68k/%.c
	$(COMPILER) $(KOS_CFLAGS) $(KOS_LOCAL_CFLAGS) -fno-gcse -D_arch_$(KOS_ARCH) $(KOS_ALL_INCS) -c $< -o $@

%.o: ../cz80/%.c
	$(COMPILER) $(KOS_CFLAGS) $(KOS_LOCAL_CFLAGS) -fno-gcse -D_arch_$(KOS_ARCH) $(KOS_ALL_INCS) -c $< -o $@

%.o: ../cpu/%.c
	$(COMPILER) $(KOS_CFLAGS) $(KOS_LOCAL_CFLAGS) -D_arch_$(KOS_ARCH) $(KOS_ALL_INCS) -c $< -o $@

%.o: ../sound/%.c
	$(COMPILER) $(KOS_CFLAGS) $(KOS_LOCAL_CFLAGS) -D_arch_$(KOS_ARCH) $(KOS_ALL_INCS) -c $< -o $@

fame.o: ../fame/fame.s
	$(KOS_AS) -little $< -o $@
        
$(GSRC): ../m68k/m68kmake
	cd ../m68k && ./m68kmake

../m68k/m68kmake: ../m68k/m68kmake.c
	gcc ../m68k/m68kmake.c -o ../m68k/m68kmake

$(BIN): $(TARGET)
	$(KOS_STRIP) $(TARGET)
	$(KOS_OBJCOPY) -R .stack -O binary $(TARGET) $(BIN)

run: $(TARGET)
	dc-tool -i roms.iso -x $(TARGET)

$(FIRSTREAD): $(BIN)
	$(KOS_CCPLUS_BASE)/bin/sh-elf-scramble $(BIN) $(FIRSTREAD)

dist: $(TARGET) $(BIN) $(FIRSTREAD)

romdisk.img:
	$(KOS_GENROMFS) -f romdisk.img -d romdisk -v

romdisk.o: romdisk.img
	$(KOS_BASE)/utils/bin2o/bin2o romdisk.img romdisk romdisk.o

newcd: $(BIN)
	mv $(BIN) disc/data/1st_read.bin
	mkisofs -l -J -R -o disc/session1.iso disc/data
	rm disc/data/1st_read.bin
	cdrecord dev=$(CDRECORD_DEV) speed=4 gracetime=2 -tao -multi -eject -xa disc/session1.iso
	rm disc/session?.iso

mscd: $(BIN)
	mv $(BIN) disc/data/1st_read.bin
	mkisofs -C `cdrecord dev=$(CDRECORD_DEV) -msinfo` -M $(CDRECORD_DEV) -l -J -R -o disc/session2.iso disc/data
	rm disc/data/1st_read.bin
	cdrecord dev=$(CDRECORD_DEV) speed=4 gracetime=2 -tao -multi -eject -xa disc/session2.iso
	rm disc/session?.iso

mscd_noimport: $(BIN)
	mv $(BIN) disc/data/1st_read.bin
	mkisofs -C `cdrecord dev=$(CDRECORD_DEV) -msinfo` -l -J -R -o disc/session2.iso disc/data
	rm disc/data/1st_read.bin
	cdrecord dev=$(CDRECORD_DEV) speed=4 gracetime=2 -tao -multi -eject -xa disc/session2.iso
	rm disc/session?.iso

selfboot: $(FIRSTREAD)
	mkisofs -G disc/IP.BIN -l -J -R -o disc/session1.iso disc/data $(FIRSTREAD)
	dd if=disc/session1.iso bs=2048 count=17 of=disc/session2.iso
	dd if=/dev/zero bs=2048 count=283 >> disc/session2.iso
	cdrecord dev=$(CDRECORD_DEV) speed=4 gracetime=2 -tao -multi -xa disc/session1.iso
	cdrecord dev=$(CDRECORD_DEV) speed=4 gracetime=2 -tao -eject -xa disc/session2.iso
	rm disc/session?.iso
	
sbi:
	rm -r Inducer
	mkdir Inducer
	mkdir Inducer/Images
	mkdir Inducer/Emus
	mkdir Inducer/$(NAME)
	cp README.txt Inducer/.
	cp $(NAME).png Inducer/Images/.
	cp $(NAME).bin Inducer/Emus/.
	cp $(NAME).dxl Inducer/Emus/.
	cp -R romdisk/data Inducer/$(NAME)
	rm Inducer/$(NAME)/data/ROMS/*

